import request from '@/utils/request'
import type { Contract, ContractCategory } from '@/types/contract'
import { ContractItem, ApiResponse } from '@/types/index'
import config from '@/config/index'

export const contractApi = {
  // 获取销售合同列表
  getContractList: (params: any): Promise<ApiResponse<Contract[]>> => {
    return request({
      url: '/api/contract/get_contract',
      method: 'get',
      params,
    })
  },

  // 销售合同归档操作
  contractArchive: (data: any): Promise<ApiResponse<any>> => {
    return request({
      url: '/api/contract/contract_archive',
      method: 'post',
      data,
    })
  },

  // 销售合同中止操作
  contractStop: (data: any): Promise<ApiResponse<any>> => {
    return request({
      url: '/api/contract/contract/stop',
      method: 'post',
      data,
    })
  },

  // 销售合同作废操作
  contractVoid: (data: any): Promise<ApiResponse<any>> => {
    return request({
      url: '/api/contract/contract/void',
      method: 'post',
      data,
    })
  },

  // 获取合同分类列表
  getContractCategories: (): Promise<ApiResponse<ContractCategory[]>> => {
    return request({
      url: '/api/contract/cate/set',
      method: 'get',
    })
  },

  // 合同管理相关接口（从 contract/contract.ts 合并）
  // 获取合同列表
  getContractDetailList: (params?: any): Promise<ApiResponse<Contract[]>> => {
    return request({
      url: '/api/contract/contract/list',
      method: 'get',
      params,
    })
  },

  // 获取归档合同列表
  getArchiveList: (params?: any): Promise<ApiResponse<Contract[]>> => {
    return request({
      url: '/api/contract/contract/archive_list',
      method: 'get',
      params,
    })
  },

  // 获取中止合同列表
  getStopList: (params?: any): Promise<ApiResponse<Contract[]>> => {
    return request({
      url: '/api/contract/contract/stop_list',
      method: 'get',
      params,
    })
  },

  // 获取作废合同列表
  getVoidList: (params?: any): Promise<ApiResponse<Contract[]>> => {
    return request({
      url: '/api/contract/contract/void_list',
      method: 'get',
      params,
    })
  },

  /**
   * 查看合同详情
   * @param params 请求参数
   * @returns Promise
   */
  viewContract: (params: { id: string | number }): Promise<ApiResponse<Contract>> => {
    return request({
      url: '/api/contract/contract/view',
      method: 'get',
      params,
    })
  },

  // 获取合同选项（用于下拉选择）
  getContractOptions: (params?: any): Promise<ApiResponse<any[]>> => {
    return request({
      url: '/api/contract/contract/options',
      method: 'get',
      params,
    })
  },

  // 新增合同
  addContract: (data: any): Promise<ApiResponse<any>> => {
    return request({
      url: '/api/contract/contract/add',
      method: 'post',
      data,
    })
  },

  // 编辑合同
  editContract: (data: any): Promise<ApiResponse<any>> => {
    return request({
      url: '/api/contract/contract/edit',
      method: 'put',
      data,
    })
  },

  // 删除合同
  deleteContract: (data: any): Promise<ApiResponse<any>> => {
    return request({
      url: '/api/contract/contract/del',
      method: 'delete',
      data,
    })
  },

  // 更新合同状态
  updateContractStatus: (data: any): Promise<ApiResponse<any>> => {
    return request({
      url: '/api/contract/contract/status',
      method: 'post',
      data,
    })
  },

  // 合同审核
  auditContract: (data: any): Promise<ApiResponse<any>> => {
    return request({
      url: '/api/contract/contract/audit',
      method: 'post',
      data,
    })
  },

  // 合同归档
  archiveContract: (data: any): Promise<ApiResponse<any>> => {
    return request({
      url: '/api/contract/contract/archive',
      method: 'post',
      data,
    })
  },

  // 合同续签
  renewContract: (data: any): Promise<ApiResponse<any>> => {
    return request({
      url: '/api/contract/contract/renew',
      method: 'post',
      data,
    })
  },

  // 合同统计
  getContractStatistics: (params?: any): Promise<ApiResponse<any>> => {
    return request({
      url: '/api/contract/contract/statistics',
      method: 'get',
      params,
    })
  },

  // 导出合同
  exportContract: (params?: any): Promise<Blob> => {
    return request({
      url: '/api/contract/contract/export',
      method: 'get',
      params,
      responseType: 'blob',
    })
  },
}
